//This file contains the cross-origin middleware.
import 'dart:io';

import 'package:simple_server/src/context.dart';
import 'package:simple_server/src/middleware/middleware.dart';

class CrossOriginMiddleware<T extends Context> extends Middleware<T> {
  @override
  Future<void> call(T context, Next next) async {
    context.response.headers.set(
      HttpHeaders.accessControlAllowOriginHeader,
      '*',
    );
    context.response.headers.set(
      HttpHeaders.accessControlAllowMethodsHeader,
      'GET, POST, PUT, DELETE, OPTIONS',
    );
    context.response.headers.set(
      HttpHeaders.accessControlAllowHeadersHeader,
      'Content-Type',
    );
    if (context.method == 'options') {
      context.noContent();
      return;
    }
    await next();
  }
}
